<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->
    <!-- 文件选择框 -->
    <input type="file" hidden ref="file" @change="onFileChange"/>
    <!-- 个人信息 -->
    <van-cell class="avatar-cell" title="头像" is-link center @click="$refs.file.click()">
      <van-image
        class="avatar"
        round
        fit="cover"
        :src="userProfile.photo"
      />
    </van-cell>
    <van-cell title="昵称" @click="isUpdateUserNameShow = true" :value="userProfile.name" is-link />
    <van-cell title="性别" @click="isUpdateUserGenderShow = true" :value="userProfile.gender === 0 ? '男' : '女'" is-link />
    <van-cell title="生日" @click="isUpdateBirthdayShow = true" :value="userProfile.birthday" is-link />
    <!-- /个人信息 -->

    <!-- 编辑昵称 -->
    <van-popup
      v-model="isUpdateUserNameShow"
      position="bottom"
      style="height: 100%;" >

      <updateName
      @close="isUpdateUserNameShow = false"
      v-if="isUpdateUserNameShow"
      v-model="userProfile.name"
      ></updateName>
    </van-popup>
    <!-- 编辑性别 -->
    <van-popup
      v-model="isUpdateUserGenderShow"
      position="bottom"
      style="height: 70%;" >

      <updateGender
      @close="isUpdateUserGenderShow = false"
      v-if="isUpdateUserGenderShow"
      v-model="userProfile.gender" ></updateGender>
    </van-popup>

    <!-- 编辑生日 -->
     <van-popup
      v-model="isUpdateBirthdayShow"
      position="bottom"
      style="height: 70%;" >

      <updateBirthday
      @close="isUpdateBirthdayShow = false"
      v-if="isUpdateBirthdayShow"
      v-model="userProfile.birthday" ></updateBirthday>
    </van-popup>

    <!-- 编辑头像 -->
    <van-popup
      v-model="isUpdatePhotoShow"
      position="bottom"
      style="height: 100%;" >

      <updatePhoto
      @close="isUpdatePhotoShow = false"
      v-if="isUpdatePhotoShow"
      @update-photo="userProfile.photo = $event"
      :img="img" ></updatePhoto>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import updateName from './components/update-name'
import updateGender from './components/update-gender'
import updateBirthday from './components/update-birthday'
import updatePhoto from './components/update-photo'
export default {
  name: 'UserProfile',
  components: {
    updateName,
    updateGender,
    updateBirthday,
    updatePhoto
  },
  props: {},
  data () {
    return {
      userProfile: {},
      isUpdateUserNameShow: false,
      isUpdateUserGenderShow: false,
      isUpdateBirthdayShow: false,
      isUpdatePhotoShow: false,
      img: null
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadUserProfile()
  },
  mounted () {},
  methods: {
    async loadUserProfile() {
      try {
        const { data } = await getUserProfile()
        console.log(data)
        this.userProfile = data.data
      } catch (error) {
        this.$toast('获取用户信息失败！')
      }
    },
    onFileChange() {
      //    console.log(this.$refs.file.files)
      const file = this.$refs.file.files[0]
      const blob = window.URL.createObjectURL(file)
      this.img = blob
      this.isUpdatePhotoShow = true
      //    console.log(blob)
      // file-input 如果选了同一个文件不会触发 change 事件
      // 解决办法就是每次使用完毕，把它的 value 清空
      this.$refs.file.value = ''
    }
  }
}
</script>

<style scoped lang="less">
.user-profile {
  .avatar-cell {
    .van-cell__value {
      display: flex;
      flex-direction: row-reverse;
    }
    .avatar {
      width: 60px;
      height: 60px;
    }
  }
  .van-popup{
      background-color:#f5f7f9;
  }
}
</style>
